package juc;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

public class TestThreadPool {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService pool = Executors.newFixedThreadPool(10);
//        ThreadPoolDemo tpd = new ThreadPoolDemo();
//        for (int i = 0; i < 10; i++) {
//            pool.submit(tpd);
//        }
        List<Future> list = new ArrayList<>();
        for (int j = 0; j < 10; j++) {
            Future<Integer> future = pool.submit(()->{
                int sum = 0;
                for (int i = 0; i <= 100; i++) {
                    sum += i;
                }
                return sum;
            });
            list.add(future);
        }
        for (Future future : list) {
            System.out.println(future.get());
        }
        pool.shutdown();
    }
}

class ThreadPoolDemo implements Runnable {

    private int i = 0;

    @Override
    public void run() {
        while (i <= 100) {
            System.out.println(Thread.currentThread().getName() + ": " + i++);
        }
    }
}
